﻿Shader "Custom/Ice"
{
    Properties
    {
        [HDR]_Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        [NoScaleOffset][Normal] _Normal("法线", 2D) = "bump" {}
        _NormalScale("法线强度", Float) = 1
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
        [Header(Refract)]
        [Space]
        _RefractRatio("折射强度", Range(0,1)) = 0.5
        [Header(Fresnel)]
        [Space]
        _FresnelPower("FresnelPower", Float) = 2
        _FresnelScale("FresnelScale", Float) = 1
        _FresnelBias("FresnelBias", Float) = 0
    }

    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent"}
        LOD 200

        GrabPass{}
		zwrite off

        CGPROGRAM

        #pragma surface surf Standard fullforwardshadows vertex:vert alpha
        #pragma target 3.0

        sampler2D _MainTex;
        sampler2D _Normal;

        struct Input
        {
            float2 uv_MainTex;
            float4 tangent;
            float3 normalWS;
            float3 viewDir;
            float3 worldPos;
        };

        void vert(inout appdata_full v, out Input i)
		{
			UNITY_INITIALIZE_OUTPUT(Input, i);

            // 将法线从模型空间变换到世界空间
            half3 normalWS = UnityObjectToWorldNormal(v.normal);
            i.tangent = v.tangent;
            i.normalWS = normalWS;
        }

        half _NormalScale;

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        sampler2D _CameraDepthTexture;
		sampler2D _GrabTexture;
		half4 _GrabTexture_TexelSize;

        half _RefractRatio;

        half _FresnelPower;
        half _FresnelScale;
        half _FresnelBias;

        // 折射 i:入射光线方向 n:表面法线 eta:折射相对系数
        // I和N之间的角度太大则返回(0, 0, 0)
        float3 refract(float3 i, float3 n, float eta)
        {
            float cosi = dot(-i, n);
            float cost2 = 1 - eta * eta * (1 - cosi * cosi);
            float3 t = eta * i + n * (eta * cosi - sqrt(cost2));
            return t * step(0, cost2); // t * (int)(cost2 > 0)
        }

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            IN.normalWS = normalize(IN.normalWS);
            IN.viewDir = normalize(IN.viewDir);

            float4 tangentWorld = float4(UnityObjectToWorldDir(IN.tangent.xyz), IN.tangent.w);
            float3x3 tangentToWorld = CreateTangentToWorldPerVertex(IN.normalWS, tangentWorld.xyz, tangentWorld.w);

            float3 tangentNormal = UnpackNormalWithScale(tex2D(_Normal, IN.uv_MainTex), _NormalScale);
            float3 normalWorld = mul(tangentNormal, tangentToWorld);

            // 世界坐标观察方向   
            half3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - IN.worldPos);

            // 折射
            half3 refraction = refract(-viewDirection, normalWorld, _RefractRatio);

            // 折射后的片段位置
            half3 refractionPositionWS = _WorldSpaceCameraPos.xyz + refraction;
            half4 refractionPositionCS = UnityWorldToClipPos(refractionPositionWS);
            half4 refractionUV = ComputeNonStereoScreenPos(refractionPositionCS);
            half4 SceneColor = tex2D(_GrabTexture, refractionUV.xy / refractionUV.w);

            fixed4 MainColor = tex2D(_MainTex, IN.uv_MainTex) * _Color;

            // 菲涅耳
            half fresnel = saturate(_FresnelBias + _FresnelScale * pow(1.0f + dot(-viewDirection, normalWorld), _FresnelPower));
            SceneColor = lerp(SceneColor, MainColor, fresnel);

            o.Albedo = SceneColor;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = _Color.a;
            o.Normal = tangentNormal;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
